<template>
  <div class="app-container">
    <!-- 操作区 -->
    <el-row>
      <el-col :span="12" class="elCol1">
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item label="根据姓名">
            <el-input v-model="uname" placeholder="查询姓名"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSeach">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="toAdd">添加</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="编号" width="95">
        <template slot-scope="scope">{{ scope.$index+1 }}</template>
      </el-table-column>
      <el-table-column label="姓名" width="120">
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column label="性别" width="120" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.sex==1?"男":"女"}}</span>
        </template>
      </el-table-column>
      <el-table-column label="密码" width="300" align="center">
        <template slot-scope="scope">{{ scope.row.pwd }}</template>
      </el-table-column>

      <el-table-column label="生日" width="200" align="center">
        <template slot-scope="scope">{{ scope.row.birth }}</template>
      </el-table-column>
      <el-table-column class-name="status-col" label="电话" width="250" align="center">
        <template slot-scope="scope">{{ scope.row.phone}}</template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="Email">
        <template slot-scope="scope">
          <i class="el-icon-time"/>
          <span>{{ scope.row.email }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="操作" width="250">
        <template slot-scope="scope">
          <el-button type="warning" @click="toEdit(scope.$index)">编辑</el-button>
          <el-button type="danger" @click="onDelete(scope.$index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div>
      <!-- 分页导航区 -->
      <el-row>
        <el-col :span="24">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="current"
            :page-sizes="[3, 5, 8, 12]"
            :page-size="size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          ></el-pagination>
        </el-col>
      </el-row>
    </div>
    <!-- 编辑用户弹窗 -->
    <el-dialog
      :title="centerDialogVisible?'编辑用户':'添加用户'"
      :visible.sync="updtUserswitch"
      width="30%"
      center
    >
      <el-form>
        <el-form-item label="用户名" prop="name">
          <el-input v-model="editUser.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="editUser.sex">
            <el-radio :label="1">男</el-radio>
            <el-radio :label="2">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
          <el-input
            placeholder="请输入新密码"
            v-model="editUser.pwd"
            :type="pwdType"
            name="password"
            auto-complete="on"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="pwd2">
          <el-input
            placeholder="请再次输入新密码"
            v-model="pwd2"
            :type="pwdType"
            name="password"
            auto-complete="on"
            @blur="pwdjudge"
          ></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input el-input v-model="editUser.phone" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="生日" prop="birth">
          <el-date-picker
            v-model="editUser.birth"
            type="date"
            placeholder="选择出生日期"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="Email" prop="email">
          <el-input v-model="editUser.email" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="onCancel">取 消</el-button>
        <el-button v-if="centerDialogVisible" type="primary" @click="onEdit">确定编辑</el-button>
        <el-button v-else type="primary" @click="onAdd">确定添加</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      list: null,
      listLoading: true,
      centerDialogVisible: false,
      updtUserswitch: false,
      editUser: {},
      index: "",
      pwd2: "",
      pwdType: "password",
      uname: "",
      current: 1, //当前页;首页=1
      size: 5, //每页再显示5个
      total: 0 //总数据量
    };
  },
  methods: {
    async fetchData() {
      this.listLoading = true;
      const res = await this.$http.post("admin/AdminUser/list");
      let arr = [];
      for (let key in res.data.data) {
        arr.push(res.data.data[key]);
      }
      this.list = arr;
      this.listLoading = false;
    },
    onSeach() {
      let res = this.$http
        .post(`admin/AdminUser/detail`, this.uname)
        .then(res => {
          this.listLoading = true;
          this.listLoading = false;
        })
        .catch(err => {
          this.$message({
            type: "error",
            message: "查询" + err.msg
          });
        });
    },
    handleSizeChange(val) {
      this.size = val;
      this.fetchData();
      console.log(this.size);
    },
    handleCurrentChange(val) {
      this.current = val;
      this.fetchData();
    },
    toAdd() {
      this.centerDialogVisible = false;
      this.editUser = {};
      this.updtUserswitch = true;
      this.pwd2 = "";
    },
    onAdd() {
      let res = this.$http
        .post(`admin/AdminUser/create`, this.editUser)
        .then(response => {
          this.updtUserswitch = false;
          this.$message({
            type: "success",
            message: "添加成功!"
          });
          this.fetchData();
        })
        .catch(err => {
          this.centerDialogVisible = false;
        });
    },
    toEdit(idx) {
      this.index = idx + 1;
      this.editUser = this.list[idx];
      this.centerDialogVisible = true;
      this.updtUserswitch = true;
    },
    onEdit() {
      let res = this.$http
        .post(`admin/AdminUser/update/${this.index}`, this.editUser)
        .then(response => {
          this.$message({
            type: "success",
            message: "修改成功!"
          });
          this.editUser = [];
          this.centerDialogVisible = false;
          this.updtUserswitch = false;
          this.fetchData();
        })
        .catch(err => {
          this.$message({
            type: "error",
            message: err.msg
          });
          this.centerDialogVisible = false;
          this.updtUserswitch = false;
        });
    },
    onDelete(idx) {
      let utmp = this.list[idx];
      this.$confirm(
        "此操作将永久删除" + utmp.name + "的信息, 是否继续?",
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(async () => {
          const res = await this.$http.post(
            `admin/AdminUser/delete/${idx + 1}`
          );
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.fetchData();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    pwdjudge() {
      if (this.editUser.pwd !== this.pwd2) {
        this.$message.error("两次密码不一致");
        this.pwd2 = "";
      }
    },
    onCancel() {
      this.updtUserswitch = false;
      this.centerDialogVisible = false;
      this.editUser = {};
      this.pwd2 = "";
    }
  },

  created() {
    this.fetchData();
  }
};
</script>

